package com.ccmusic.dao.impl;

import java.util.List;

import com.ccmusic.dao.MusicDao;
import com.ccmusic.entity.Music;
import com.mysql.jdbc.StreamingNotifiable;

public class MusicDaoImpl extends BaseDao implements MusicDao {

	/**
	 * 增加音乐的方法
	 * 
	 * @param music
	 *            音乐对象,mid自增
	 * @return
	 */
	@Override
	public boolean addMusic(Music music) {
		String sql = "insert into music values(null,?,?,?,?,?,?)";
		int n = this.execute(sql, music.getTid(), music.getMname(), music.getMlike(), music.getUrl(),
				music.getCollect(), music.getDownload());
		if (n > 0) {
			return true;
		}
		return false;
	}

	/**
	 * 获取数据库中所有的音乐列表
	 * 
	 * @return 返回一个音乐的集合
	 */
	@Override
	public List<Music> getAllMusic() {
		String sql = "select * from music";
		List<Music> list = (List<Music>) this.select(sql, Music.class);
		if (list.size() > 0) {
			return list;
		}
		return null;
	}

	/**
	 * 根据音乐名模糊查询音乐，返回音乐集合
	 * 
	 * @return
	 */
	@Override
	public List<Music> getMusicByLikeName(String mname) {
		String sql = "select * from music where mname like?";
		List<Music> list = (List<Music>) this.select(sql, Music.class, "%" + mname + "%");
		if (list.size() > 0) {
			return list;
		}
		return null;
	}

	/**
	 * 根据音乐名字删除音乐
	 * 
	 * @param mname
	 *            音乐名
	 * @return 布尔类型
	 */
	@Override
	public boolean deleteMusicByName(String mname) {
		String sql = "delete from music where mname=?";
		int n = this.execute(sql, mname);
		if (n > 0) {
			return true;
		}
		return false;
	}

	/**
	 * 根据音乐id删除音乐
	 * 
	 * @param mname
	 *            音乐名
	 * @return 布尔类型
	 */
	@Override
	public boolean deleteMusicByMid(int mid) {
		String sql = "delete from music where mid=?";
		int n = this.execute(sql, mid);
		if (n > 0) {
			return true;
		}
		return false;
	}

	/**
	 * 更新音乐名和音乐url
	 * 
	 * @param music
	 *            更新后的music对象
	 */
	@Override
	public boolean updateMusic(Music music) {
		String sql = "update music set mname=?,url=? where mid=?";
		int n = this.execute(sql, music.getMname(), music.getUrl(), music.getMid());
		return n > 0;
	}

	/**
	 * 根据音乐id点赞
	 * 
	 * @param mid
	 *            音乐id
	 * @return 点赞成功返回true；点赞失败返回false
	 */
	@Override
	public boolean likeIt(String mid) {
		String sql = "update music set mlike=mlike+1 where url=?";
		int rsCode = this.execute(sql, mid);
		return rsCode > 0;
	}

	/**
	 * 根据音乐id取消点赞
	 * 
	 * @param mid
	 *            音乐id
	 * @return 取消点赞成功返回true；取消点赞失败返回false
	 */
	@Override
	public boolean dislikeIt(String mid) {
		String sql = "update music set mlike=mlike-1 where url=?";
		int rsCode = this.execute(sql, mid);
		return rsCode > 0;
	}

	/**
	 * 根据音乐url获取音乐
	 * 
	 * @param url
	 *            音乐url
	 * @return 如果存在返回该音乐对象，否则返回null
	 */
	@Override
	public Music getMusicByMid(String url) {
		String sql = "select * from music where url=?";
		List<Music> musics = (List<Music>) this.select(sql, Music.class, url);
		if (musics.size() > 0)
			return musics.get(0);
		return null;
	}

}
